﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace wwl.DataAccess.Migrations.Rbac
{
    /// <inheritdoc />
    public partial class Rbac_AddUserRolePermissionTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "rbac_permission",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    perm_code = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    perm_name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    perm_type = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    parent_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    resource_path = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
                    tenant_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    created_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    created_by = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    remarks = table.Column<string>(type: "text", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rbac_permission", x => x.id);
                    table.CheckConstraint("ck_rbac_permission_type", "perm_type IN ('MENU', 'OPERATION', 'BUTTON')");
                    table.ForeignKey(
                        name: "FK_rbac_permission_rbac_permission_parent_id",
                        column: x => x.parent_id,
                        principalTable: "rbac_permission",
                        principalColumn: "id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "rbac_role",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    role_code = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    role_name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    role_type = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    tenant_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    created_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    created_by = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    remarks = table.Column<string>(type: "text", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rbac_role", x => x.id);
                    table.CheckConstraint("ck_rbac_role_type", "role_type IN ('SYSTEM', 'BUSINESS')");
                });

            migrationBuilder.CreateTable(
                name: "rbac_user",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    username = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    password_hash = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: false),
                    full_name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    email = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    phone = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
                    status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    tenant_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    created_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    created_by = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    remarks = table.Column<string>(type: "text", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rbac_user", x => x.id);
                    table.CheckConstraint("ck_rbac_user_status", "status IN ('ENABLED', 'DISABLED', 'LOCKED')");
                });

            migrationBuilder.CreateTable(
                name: "rbac_role_permission",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    role_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    permission_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    tenant_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    created_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    created_by = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    remarks = table.Column<string>(type: "text", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rbac_role_permission", x => x.id);
                    table.ForeignKey(
                        name: "FK_rbac_role_permission_rbac_permission_permission_id",
                        column: x => x.permission_id,
                        principalTable: "rbac_permission",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_rbac_role_permission_rbac_role_role_id",
                        column: x => x.role_id,
                        principalTable: "rbac_role",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "rbac_user_role",
                columns: table => new
                {
                    id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    user_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    role_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    tenant_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    created_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    updated_time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    created_by = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    remarks = table.Column<string>(type: "text", nullable: false),
                    is_deleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_rbac_user_role", x => x.id);
                    table.ForeignKey(
                        name: "FK_rbac_user_role_rbac_role_role_id",
                        column: x => x.role_id,
                        principalTable: "rbac_role",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_rbac_user_role_rbac_user_user_id",
                        column: x => x.user_id,
                        principalTable: "rbac_user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_rbac_permission_parent_id",
                table: "rbac_permission",
                column: "parent_id");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_permission_perm_code",
                table: "rbac_permission",
                column: "perm_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_rbac_permission_resource_path",
                table: "rbac_permission",
                column: "resource_path");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_permission_tenant_id",
                table: "rbac_permission",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_role_role_code",
                table: "rbac_role",
                column: "role_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_rbac_role_tenant_id",
                table: "rbac_role",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "IX_rbac_role_permission_permission_id",
                table: "rbac_role_permission",
                column: "permission_id");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_role_permission_tenant_id",
                table: "rbac_role_permission",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "uk_rbac_role_permission_role_perm",
                table: "rbac_role_permission",
                columns: new[] { "role_id", "permission_id" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_rbac_user_status",
                table: "rbac_user",
                column: "status");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_user_tenant_id",
                table: "rbac_user",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_user_username",
                table: "rbac_user",
                column: "username",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_rbac_user_role_role_id",
                table: "rbac_user_role",
                column: "role_id");

            migrationBuilder.CreateIndex(
                name: "ix_rbac_user_role_tenant_id",
                table: "rbac_user_role",
                column: "tenant_id");

            migrationBuilder.CreateIndex(
                name: "uk_rbac_user_role_user_role",
                table: "rbac_user_role",
                columns: new[] { "user_id", "role_id" },
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "rbac_role_permission");

            migrationBuilder.DropTable(
                name: "rbac_user_role");

            migrationBuilder.DropTable(
                name: "rbac_permission");

            migrationBuilder.DropTable(
                name: "rbac_role");

            migrationBuilder.DropTable(
                name: "rbac_user");
        }
    }
}
